package com.yizhi.android.juhe.ui;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.yizhi.android.juhe.R;
import com.yizhi.android.juhe.utils.TestUtil;
import com.yizhi.android.juhe.utils.pay.AlipayAppUtils;
import com.yizhi.android.juhe.utils.pay.MiniProgramUtils;
import com.yizhi.android.juhe.utils.pay.QuickPayUtils;
import com.zhaohang.juhe.JuHe;
import com.zhaohang.juhe.base.CallBack;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import xyz.cq.clog.CLog;
import xyz.cq.clog.dialog.HttpDialog;


/**
 * @author qian.cheng
 */
public class MainActivity extends AppCompatActivity {

    private final String productName = "兆行金科";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**
     * 如果执行错误，可能是没有安装支付宝
     *
     * @param view
     */
    public void alipayApp(View view) {
        HttpDialog httpDialog = new HttpDialog(this);
        httpDialog.show();
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("alipays://platformapi/startApp?appId=10000011&url=" + URLEncoder.encode(AlipayAppUtils.getUrl(), "UTF-8")));
            startActivity(intent);
            httpDialog.dismiss();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            httpDialog.dismiss();
        }
    }

    public void alipayCode(View view) {
        CodeActivity.startActivity(this, "支付宝付款码");
    }

    public void wXpayCode(View view) {
        CodeActivity.startActivity(this, "微信付款码");
    }

    public void quickPay(View view) {
        try {
            WebActivity.startActivity(this, "快捷支付", QuickPayUtils.getUrl());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public void orderQuery(View view) {
        OrderActivity.startActivity(this, "订单查询");
    }


    /**
     * 跳转小程序方法
     */
    public void jumpApplet1(View view) {
        MiniProgramUtils.post(new TestUtil.CallBack1() {
            @Override
            public void onSuccess(final String result) {
                CLog.log().i("MiniProgramUtils", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    JuHe.smallProgramJump(MainActivity.this, new CallBack() {
                        @Override
                        public void onSuccess() {
                        }

                        @Override
                        public void onFail(int i, String s) {
                            CLog.log().i("smallProgramJump", "errorcode:" + i + "  msg:" + s);
                        }
                    }, jsonObject.get("json").toString(), productName);
                } catch (JSONException ignored) {
                }
            }

            @Override
            public void onFail(Throwable t) {
                CLog.log().d("请求异常", t);
            }
        });
    }

}
